package com.pandy.disruptor.senior;

import com.lmax.disruptor.EventTranslator;
import com.lmax.disruptor.dsl.Disruptor;

import java.util.Random;
import java.util.UUID;
import java.util.concurrent.CountDownLatch;

/**
 * @description: 消息生产者
 * @author: panhong
 * @date: 2025/4/10
 */
public class GoodsPublisher implements Runnable {

    private Disruptor<Goods> disruptor;
    private CountDownLatch latch;

    private static final int PUBLISH_COUNT = 1;

    public GoodsPublisher(Disruptor<Goods> disruptor, CountDownLatch latch) {
        this.disruptor = disruptor;
        this.latch = latch;
    }

    @Override
    public void run() {
        GoodsEventTranslator eventTranslator = new GoodsEventTranslator();
        for( int i=0; i<PUBLISH_COUNT; i++) {
            // 提交发布操作
            disruptor.publishEvent(eventTranslator);
        }
        latch.countDown();
    }
}

class GoodsEventTranslator implements EventTranslator<Goods> {

    Random random = new Random();
    @Override
    public void translateTo(Goods event, long sequence) {
        event.setPrice(random.nextDouble() * 999);
    }
}
